import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class GameWin extends JFrame {

    //存储金块，石块
    List<Object> objectList = new ArrayList<>();
    Bg bg = new Bg();
    Line line = new Line(this);
    {
        for (int i =0;i<3;i++){
            objectList.add(new Gold());
        }
        for (int i =0;i<3;i++){
            objectList.add(new Rock());
        }
    }
    //定义这一张画布
    Image offScreenImage;
    void launch(){
        this.setVisible(true );//窗口可见
        this.setSize(768,1000);//窗口大小
        this.setLocationRelativeTo(null);
        this.setTitle("黄金矿工");
        setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗口

        addMouseListener(new MouseAdapter() {//创建一个鼠标事件
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if(e.getButton()==1) {
                    line.state = 1;
                }//鼠标左键1，右键2，混轮3
            }
        });

        while(true){
            repaint();//让红线不停绘制
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g){//主程序中调用类中方法
        //绘制窗口元素
        //双缓存技术
        offScreenImage = this.createImage(768,1000);
        Graphics gImage = offScreenImage.getGraphics();
        bg.paintSelf(gImage);
        line.paintSelf(gImage);
        // 增强for循环
        for(Object obj:objectList){//绘制金块，石块
            obj.paintSelf(gImage);
        }
        g.drawImage(offScreenImage,0,0,null);

    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();

    }
}
